package com.yzx.lifeassistants.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.romainpiel.titanic.library.Titanic;
import com.romainpiel.titanic.library.TitanicTextView;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.UMImage;
import com.yalantis.contextmenu.lib.ContextMenuDialogFragment;
import com.yalantis.contextmenu.lib.MenuObject;
import com.yalantis.contextmenu.lib.MenuParams;
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener;
import com.yzx.lifeassistants.GlobalParams;
import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.adapter.CustomPagerAdapter;
import com.yzx.lifeassistants.adapter.CyclePagerAdapter;
import com.yzx.lifeassistants.adapter.ShareListAdapter;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.bean.SecondHandGoods;
import com.yzx.lifeassistants.bean.SharePlatform;
import com.yzx.lifeassistants.common.CommonConstant;
import com.yzx.lifeassistants.fragment.SecondHandContactInfoFragment;
import com.yzx.lifeassistants.fragment.SecondHandItemInfoFragment;
import com.yzx.lifeassistants.utils.AppUtils;
import com.yzx.lifeassistants.utils.FileUtils;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.ScreenUtils;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.utils.TypefacesUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;
import com.yzx.lifeassistants.view.widget.CycleViewPager;

/**
 * @Description: 二手物品详情界面
 * @author: yzx
 * @time: 2015-11-13 上午11:05:22
 */
@SuppressLint("InflateParams")
public class SecondHandDetailActivity extends BaseActivity implements
		OnClickListener, OnPageChangeListener, OnMenuItemClickListener {
	private RelativeLayout topRL;
	private ImageButton topBackBtn;// 顶部返回按钮
	private ImageButton moreBtn;// 顶部分享按钮
	private RelativeLayout picRL;// 图片布局
	private CycleViewPager picPager;// 轮播图片
	private LinearLayout dotLL;// 图片下方的小点
	private TitanicTextView noPivTV;// 暂无图片
	private TextView titleTV;// 标题
	private RadioButton itemRB;// 物品信息按钮
	private RadioButton contactRB;// 联系信息按钮
	private ViewPager infoPager;// 信息

	private SecondHandGoods secondHandInfo;// 闲置物品
	private List<String> picUrlList;// 图片URL列表
	private CyclePagerAdapter picAdapter;// 图片适配器
	private CustomPagerAdapter infoAdapter;// 信息适配器
	public static final int TAB_ITEM_INFO = 0;// 物品信息
	public static final int TAB_CONTACT_INFO = 1;// 联系信息

	// //////////QQ,QZone使用友盟分享,其余使用ShareSDK分享//////////////
	private ShareParams shareParams;// 分享内容
	private PlatformActionListener listener;// 分享回调监听
	private Bitmap bitmap;// 分享图片
	private String shareImagePath;// 分享图片路径
	private UMSocialService mShareController;// 友盟分享控制器
	private SnsPostListener snsPostListener;// 友盟分享回调监听

	private View shareView;// 分享弹框
	private AlertDialog theDialog;// 分享弹框

	private CircularLoadingDialog dialog;
	private FragmentManager fragmentManager;//
	private ContextMenuDialogFragment mMenuDialogFragment;//
	private int topHeight;// 顶部高度
	private MenuObject loveMenu;// 收藏
	private Boolean isLove;// 是否收藏

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second_hand_detail);
		init();
		initView();
		setListener();
		initShareDialog();
		initCycleImage();
		initMenuFragment();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化上个界面传来的值
	 */
	private void init() {
		Intent intent = getIntent();
		if (null != intent) {
			secondHandInfo = (SecondHandGoods) intent
					.getSerializableExtra(CommonConstant.TO_SECOND_HAND_DETAIL_ACTIVITY_KEY);
		}
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		topRL = (RelativeLayout) findViewById(R.id.second_hand_detail_top_rl);
		topBackBtn = (ImageButton) findViewById(R.id.second_hand_detail_back_btn);
		moreBtn = (ImageButton) findViewById(R.id.second_hand_detail_more_btn);
		picRL = (RelativeLayout) findViewById(R.id.second_hand_detail_pic_rl);
		picPager = (CycleViewPager) findViewById(R.id.second_hand_detail_pic_viewpager);
		dotLL = (LinearLayout) findViewById(R.id.second_hand_detail_pic_dot_ll);
		noPivTV = (TitanicTextView) findViewById(R.id.second_hand_detail_no_pic_tv);
		titleTV = (TextView) findViewById(R.id.second_hand_detail_info_title_tv);
		itemRB = (RadioButton) findViewById(R.id.second_hand_detail_info_item_rb);
		contactRB = (RadioButton) findViewById(R.id.second_hand_detail_info_contact_rb);
		infoPager = (ViewPager) findViewById(R.id.second_hand_detail_info_viewpager);

	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	@SuppressWarnings("deprecation")
	private void setListener() {
		topBackBtn.setOnClickListener(this);
		moreBtn.setOnClickListener(this);
		itemRB.setOnClickListener(this);
		contactRB.setOnClickListener(this);
		infoPager.setOnPageChangeListener(this);
	}

	/**
	 * 
	 * @Description: 初始化分享框
	 */
	private void initShareDialog() {
		// 弹框
		theDialog = new AlertDialog.Builder(this, R.style.ShareDialog).create();
		theDialog.setCanceledOnTouchOutside(true);
		shareView = LayoutInflater.from(this).inflate(R.layout.dialog_share,
				null);
		List<SharePlatform> shareList = new ArrayList<SharePlatform>();
		shareList.add(new SharePlatform("QQ好友",
				R.drawable.ssdk_oks_skyblue_logo_qq_checked));
		shareList.add(new SharePlatform("QQ空间",
				R.drawable.ssdk_oks_skyblue_logo_qzone_checked));
		shareList.add(new SharePlatform("新浪微博",
				R.drawable.ssdk_oks_skyblue_logo_sinaweibo_checked));
		shareList.add(new SharePlatform("微信好友",
				R.drawable.ssdk_oks_skyblue_logo_wechat_checked));
		shareList.add(new SharePlatform("微信收藏",
				R.drawable.ssdk_oks_skyblue_logo_wechatfavorite_checked));
		shareList.add(new SharePlatform("微信朋友圈",
				R.drawable.ssdk_oks_skyblue_logo_wechatmoments_checked));
		shareList.add(new SharePlatform("更多", R.drawable.ic_share_more));
		ListView shareLV = (ListView) shareView
				.findViewById(R.id.dialog_share_lv);
		ShareListAdapter adapter = new ShareListAdapter(this, shareList);
		shareLV.setAdapter(adapter);
		shareLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long arg3) {
				switch (position) {
				case 0: {// 分享到QQ
					shareToQQ();
					break;
				}
				case 1: {// 分享到QQ空间
					shareToQzone();
					break;
				}
				case 2: {// 分享到新浪微博
					shareToSinaWeibo();
					break;
				}
				case 3: {// 分享到微信好友
					showToast("分享中···", true);
					shareToWeChatSession();
					break;
				}
				case 4: {// 分享到微信收藏
					showToast("分享中···", true);
					shareToWeChatFavorite();
					break;
				}
				case 5: {// 分享到微信朋友圈
					showToast("分享中···", true);
					shareToWeChatTimeline();
					break;
				}
				case 6: {// 更多
					share("", null);
					break;
				}
				default:
					break;
				}
			}
		});
	}

	/**
	 * 
	 * @Description: 初始化图片轮播
	 */
	private void initCycleImage() {
		picUrlList = new ArrayList<String>();
		List<BmobFile> picFileList = secondHandInfo.getPicFileList();
		if (null == picFileList || 1 > picFileList.size()) {
			picPager.setVisibility(View.GONE);
			dotLL.setVisibility(View.GONE);
			// 设置图片布局背景
			picRL.setBackgroundResource(R.color.blue_btn_bg_color);
			// 设置字体
			noPivTV.setTypeface(TypefacesUtils.get(this, "Satisfy-Regular.ttf"));
			Titanic titanic = new Titanic();
			titanic.start(noPivTV);
			noPivTV.setVisibility(View.VISIBLE);
		} else {
			noPivTV.setVisibility(View.GONE);
			picPager.setVisibility(View.VISIBLE);
			dotLL.setVisibility(View.VISIBLE);
			for (BmobFile picFile : picFileList) {
				picUrlList.add(picFile.getFileUrl(this));
			}
			picPager.setDotViewList(picUrlList.size(), R.drawable.icon_dot_on,
					R.drawable.icon_dot_off, dotLL);
			picAdapter = new CyclePagerAdapter(this, picUrlList);
			picPager.setAdapter(picAdapter);

		}
	}

	/**
	 * 
	 * @Description: 初始化菜单
	 */
	private void initMenuFragment() {
		fragmentManager = getSupportFragmentManager();
		MenuParams menuParams = new MenuParams();
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		topRL.measure(w, h);
		topHeight = topRL.getMeasuredHeight();
		menuParams.setActionBarSize(topHeight);
		menuParams.setMenuObjects(getMenuObjects());
		menuParams.setClipToPadding(false);
		menuParams.setClosableOutside(true);
		mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
		mMenuDialogFragment.setItemClickListener(this);
	}

	/**
	 * 
	 * @Description: 设置列表项
	 */
	private List<MenuObject> getMenuObjects() {
		List<MenuObject> menuObjects = new ArrayList<MenuObject>();
		int mColor = getResources().getColor(R.color.actionbar_color);
		// 关闭
		MenuObject close = new MenuObject();
		close.setResource(R.drawable.icon_menu_close);
		close.setBgColor(mColor);
		// 联系TA~
		MenuObject callMenu = new MenuObject("联系TA~");
		callMenu.setResource(R.drawable.icon_menu_call_tel);
		callMenu.setBgColor(mColor);
		callMenu.setMenuTextAppearanceStyle(R.style.TextViewStyle);
		// 收藏
		loveMenu = new MenuObject("添加收藏");
		loveMenu.setResource(R.drawable.icon_menu_runnning_love);
		loveMenu.setBgColor(mColor);
		loveMenu.setMenuTextAppearanceStyle(R.style.TextViewStyle);
		// 分享
		MenuObject shareMenu = new MenuObject("一键分享");
		shareMenu.setResource(R.drawable.icon_menu_share);
		shareMenu.setBgColor(mColor);
		shareMenu.setMenuTextAppearanceStyle(R.style.TextViewStyle);

		menuObjects.add(close);
		menuObjects.add(callMenu);
		menuObjects.add(loveMenu);
		menuObjects.add(shareMenu);
		return menuObjects;
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	private void initData() {
		dialog = new CircularLoadingDialog(this);
		moreBtn.setVisibility(View.VISIBLE);
		titleTV.setText(secondHandInfo.getTitle());
		FragmentManager fManager = getSupportFragmentManager();
		Bundle bundle = new Bundle();
		bundle.putSerializable(
				CommonConstant.FROM_SECOND_HAND_DETAIL_ACTIVITY_KEY,
				secondHandInfo);
		List<Fragment> fragments = new ArrayList<Fragment>();
		SecondHandItemInfoFragment itemInfoFragment = new SecondHandItemInfoFragment();
		// Activity传值到Fragment通过fragment.setArguments(bundle);
		itemInfoFragment.setArguments(bundle);
		fragments.add(itemInfoFragment);
		SecondHandContactInfoFragment contactInfoFragment = new SecondHandContactInfoFragment();
		contactInfoFragment.setArguments(bundle);
		fragments.add(contactInfoFragment);
		infoAdapter = new CustomPagerAdapter(fManager, fragments);
		infoPager.setAdapter(infoAdapter);
		infoPager.setOffscreenPageLimit(2);
		infoPager.setCurrentItem(TAB_ITEM_INFO);

		dialog.show();
		// 查询该用户所有的收藏闲置帖子，因此查询的是约跑表
		BmobQuery<SecondHandGoods> query = new BmobQuery<SecondHandGoods>();
		// likes是RunningInfo表中的字段，用来存储所有喜欢该信息的用户
		query.addWhereRelatedTo("secondHandLoves", new BmobPointer(
				GlobalParams.userInfo));
		query.findObjects(this, new FindListener<SecondHandGoods>() {

			@Override
			public void onSuccess(List<SecondHandGoods> list) {
				dialog.dismiss();
				for (SecondHandGoods info : list) {
					if (secondHandInfo.getObjectId().equals(info.getObjectId())) {
						isLove = true;
						loveMenu.setTitle("取消收藏");
						loveMenu.setResource(R.drawable.icon_menu_love_yes);
						return;
					}
				}
				isLove = false;
				loveMenu.setTitle("添加收藏");
				loveMenu.setResource(R.drawable.icon_menu_runnning_love);
			}

			@Override
			public void onError(int code, String msg) {
				dialog.dismiss();
				LogcatUtils.e("获取该用户收藏的闲置失败," + code + "：" + msg);
				isLove = false;
				loveMenu.setTitle("添加收藏");
				loveMenu.setBgResource(R.drawable.icon_menu_runnning_love);
			}
		});

		// //////////////////////ShareSDK分享配置////////////////////////
		shareParams = new ShareParams();
		listener = new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int code, Throwable throwable) {
				LogcatUtils.e("分享失败： " + code + " " + throwable.getMessage());
				theDialog.dismiss();
				showToast("分享失败，请重试~", false);
			}

			@Override
			public void onComplete(Platform platform, int code,
					HashMap<String, Object> map) {
				theDialog.dismiss();
				showToast("分享到" + platform.getName() + "成功~", true);
			}

			@Override
			public void onCancel(Platform arg0, int code) {
				theDialog.dismiss();
				showToast("您已取消分享~", true);
			}
		};
		// //////////////////////友盟分享配置////////////////////////
		mShareController = UMServiceFactory
				.getUMSocialService("com.umeng.share");
		// 关闭自身的Toast
		mShareController.getConfig().closeToast();
		snsPostListener = new SnsPostListener() {

			@Override
			public void onStart() {
				showToast("分享中···", true);
			}

			@Override
			public void onComplete(SHARE_MEDIA media, int code,
					SocializeEntity entity) {
				switch (code) {
				case 200: {
					showToast("分享到" + media.name() + "成功~", true);
					break;
				}
				case 40000: {
					showToast("您已取消分享~", true);
					break;
				}
				default: {
					LogcatUtils.e("分享失败： " + code);
					showToast("分享失败 ，请重试~", false);
					break;
				}
				}
				theDialog.dismiss();
			}
		};

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	/**
	 * @Description: 滑动监听
	 */
	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case TAB_ITEM_INFO: {// 物品信息
			itemRB.setChecked(true);
			break;
		}
		case TAB_CONTACT_INFO: {// 联系信息
			contactRB.setChecked(true);
			break;
		}
		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.second_hand_detail_back_btn: {// 返回
			finish();
			break;
		}
		case R.id.second_hand_detail_more_btn: {// 更多
			if (fragmentManager
					.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
				mMenuDialogFragment.show(fragmentManager,
						ContextMenuDialogFragment.TAG);
			}
			break;
		}
		case R.id.second_hand_detail_info_item_rb: {// 物品信息
			infoPager.setCurrentItem(TAB_ITEM_INFO);
			break;
		}
		case R.id.second_hand_detail_info_contact_rb: {// 联系信息
			infoPager.setCurrentItem(TAB_CONTACT_INFO);
			break;
		}
		default:
			break;
		}
	}

	// /////////////////////////分享////////////////////////////////
	/**
	 * 
	 * @Description: 弹出推荐分享框
	 */
	@SuppressLint("NewApi")
	private void showShareDialog() {
		// 获取图片
		bitmap = ScreenUtils.snapShotWithoutStatusBar(this);
		if (null != bitmap) {
			shareImagePath = FileUtils.getImagePath();
			if (null != shareImagePath) {
				FileUtils.saveBitmap(bitmap, shareImagePath);
				if (FileUtils.isFilePathExist(shareImagePath)) {
					shareParams.setImagePath(shareImagePath);
				}
			}

		}
		theDialog.show();
		theDialog.setContentView(shareView);
		Window window = theDialog.getWindow();
		window.setGravity(Gravity.BOTTOM);
		window.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		window.setAttributes(lp);
	}

	/**
	 * 
	 * @Description: 分享到QQ好友
	 */
	private void shareToQQ() {
		// ShareSDK图文分享 纯图片分享无法回调
		// shareParams.setText(CommonConstant.SHARE_CONTENT);
		// shareParams.setTitle(CommonConstant.SHARE_TITLE);
		// shareParams.setTitleUrl(CommonConstant.SHARE_TARGET_URL);
		// Platform qqPlatform = ShareSDK.getPlatform(QQ.NAME);
		// qqPlatform.setPlatformActionListener(listener);
		// qqPlatform.share(shareParams);
		// 使用友盟分享 QQ图文分享
		if (AppUtils.isInstallApplication("com.tencent.mobileqq")) {
			mShareController.setShareContent(null);
			if (null != bitmap) {
				mShareController.setShareImage(new UMImage(this, bitmap));
			}
			mShareController.postShare(this, SHARE_MEDIA.QQ, snsPostListener);
		} else {
			showToast("请先安装最新版QQ~", false);
		}
	}

	/**
	 * 
	 * @Description: 分享到空间
	 */
	private void shareToQzone() {
		// 使用ShareSDK分享 图片需要先上传服务器速度慢 空间不支持纯图片
		// shareParams.setTitle(CommonConstant.SHARE_TITLE);
		// shareParams.setTitleUrl(CommonConstant.SHARE_TARGET_URL);
		// shareParams.setText(CommonConstant.SHARE_CONTENT);
		// shareParams.setSite("Lifeassistants");
		// shareParams.setSiteUrl(CommonConstant.SHARE_TARGET_URL);
		// Platform qZonePlatform = ShareSDK.getPlatform(QZone.NAME);
		// // 设置false表示使用SSO授权方式
		// qZonePlatform.SSOSetting(false);
		// qZonePlatform.setPlatformActionListener(listener);
		// qZonePlatform.share(shareParams);
		// 使用友盟分享 QQ空间图文分享
		if (AppUtils.isInstallApplication("com.qzone")
				|| AppUtils.isInstallApplication("com.tencent.mobileqq")) {
			mShareController.setShareContent(CommonConstant.SHARE_CONTENT);
			if (null != bitmap) {
				mShareController.setShareImage(new UMImage(this, bitmap));
			}
			mShareController
					.postShare(this, SHARE_MEDIA.QZONE, snsPostListener);
		} else {
			showToast("请先安装最新版QQ空间或QQ~", false);
		}
	}

	/**
	 * 
	 * @Description: 分享到新浪微博
	 */
	private void shareToSinaWeibo() {
		// 新浪微博图文分享
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("Id", "1");// 自定义字段，整型，用于您项目中对此平台的识别符
		hashMap.put("SortId", "1");// "此平台在您分享列表中的位置，整型，数值越大越靠后"
		hashMap.put("AppKey", CommonConstant.SINA_APP_ID);// 填写您在新浪微博上注册到的AppKey
		hashMap.put("AppSecret", CommonConstant.SINA_APP_SECRET);// 填写您在新浪微博上注册到的AppSeret
		hashMap.put("RedirectUrl", CommonConstant.SINA_REDIRECT_URL);// 填写您在新浪微博上注册的RedirectUrl
		if (AppUtils.isInstallApplication("com.sina.weibo")) {
			hashMap.put("ShareByAppClient", "true");// 使用客户端进行分享
		} else {
			hashMap.put("ShareByAppClient", "false");// 不使用客户端进行分享
		}
		hashMap.put("Enable", "true");// 布尔值，标记此平台是否有效
		ShareSDK.setPlatformDevInfo(SinaWeibo.NAME, hashMap);
		if (AppUtils.isInstallApplication("com.sina.weibo")) {
			// 调用新浪微博客户端授权分享
			shareParams.setText(CommonConstant.SHARE_CONTENT);
			shareParams.setUrl(CommonConstant.SHARE_TARGET_URL);
			shareParams.setTitle(CommonConstant.SHARE_TITLE);
			shareParams.setTitleUrl(CommonConstant.SHARE_TARGET_URL);
			shareParams.setShareType(Platform.SHARE_WEBPAGE);
			Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
			// 设置false表示使用SSO授权方式
			weibo.SSOSetting(false);
			// 设置分享事件回调
			weibo.setPlatformActionListener(listener);
			weibo.share(shareParams);
		} else {
			// 调用网页授权分享
			OnekeyShare oks = new OnekeyShare();
			oks.setPlatform(SinaWeibo.NAME);
			// 显示编辑页面
			oks.setSilent(false);
			// 显示为Dialog
			oks.setDialogMode();
			oks.setText(CommonConstant.SHARE_CONTENT + "下载地址"
					+ CommonConstant.SHARE_TARGET_URL + " ");
			if (null != shareImagePath) {
				oks.setImagePath(shareImagePath);
			}
			oks.setCallback(listener);
			oks.show(this);
		}
	}

	/**
	 * 
	 * @Description: 分享到微信好友
	 */
	private void shareToWeChatSession() {
		if (AppUtils.isInstallApplication("com.tencent.mm")) {
			// 纯图片分享
			shareParams.setText(null);
			shareParams.setTitle(CommonConstant.SHARE_TITLE);
			// 分享类型 图片
			shareParams.setShareType(Platform.SHARE_IMAGE);
			Platform wxPlatform = ShareSDK.getPlatform(Wechat.NAME);
			wxPlatform.setPlatformActionListener(listener);
			wxPlatform.share(shareParams);
		} else {
			showToast("请先安装最新版微信~", false);
		}

	}

	/**
	 * 
	 * @Description: 分享到微信朋友圈
	 */
	private void shareToWeChatTimeline() {
		if (AppUtils.isInstallApplication("com.tencent.mm")) {
			// 纯图片分享
			shareParams.setText(null);
			shareParams.setTitle(CommonConstant.SHARE_TITLE);
			// 分享类型 图片
			shareParams.setShareType(Platform.SHARE_IMAGE);
			Platform wxTimeLinePlatform = ShareSDK
					.getPlatform(WechatMoments.NAME);
			wxTimeLinePlatform.setPlatformActionListener(listener);
			wxTimeLinePlatform.share(shareParams);
		} else {
			showToast("请先安装最新版微信~", false);
		}

	}

	/**
	 * 
	 * @Description: 分享到微信收藏
	 */
	private void shareToWeChatFavorite() {
		if (AppUtils.isInstallApplication("com.tencent.mm")) {
			// 纯图片分享
			shareParams.setText(null);
			shareParams.setTitle(CommonConstant.SHARE_TITLE);
			// 分享类型 图片
			shareParams.setShareType(Platform.SHARE_IMAGE);
			Platform wxFavoritePlatform = ShareSDK
					.getPlatform(WechatFavorite.NAME);
			wxFavoritePlatform.setPlatformActionListener(listener);
			wxFavoritePlatform.share(shareParams);
		} else {
			showToast("请先安装最新版微信~", false);
		}

	}

	/**
	 * 
	 * @Description: 分享更多
	 */
	private void share(String packages, Uri uri) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		if (uri != null) {
			intent.setType("image/*");
			intent.putExtra(Intent.EXTRA_STREAM, uri);
		} else {
			intent.setType("text/plain");
		}
		intent.putExtra(Intent.EXTRA_SUBJECT, "分享到");
		intent.putExtra(Intent.EXTRA_TEXT, CommonConstant.SHARE_CONTENT
				+ "下载地址" + CommonConstant.SHARE_TARGET_URL);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		if (!TextUtils.isEmpty(packages))
			intent.setPackage(packages);
		startActivity(Intent.createChooser(intent, "分享到"));
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message, Boolean isInfo) {
		if (isInfo) {
			ToastUtils.showInfoToast(this, message,
					R.id.second_hand_detail_top_ll);
		} else {
			ToastUtils.showAlertToast(this, message,
					R.id.second_hand_detail_top_ll);
		}

	}

	/**
	 * 
	 * @Description: 右菜单列表按键监听
	 */
	@Override
	public void onMenuItemClick(View clickedView, int position) {
		switch (position) {

		case 1: {// 联系TA~
			if (null == secondHandInfo || null == secondHandInfo.getPhone()
					|| "".equals(secondHandInfo.getPhone())) {
				showToast("暂无联系号码~", false);
				return;
			}
			Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
					+ secondHandInfo.getPhone()));
			startActivity(intent);
			break;
		}
		case 2: {// 收藏
			loveSecondHand();
			break;
		}
		case 3: {// 分享
			showShareDialog();
			break;
		}
		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: 收藏该闲置信息
	 */
	private void loveSecondHand() {
		dialog.show();
		BmobRelation relation = new BmobRelation();
		if (isLove) {// 取消收藏
			relation.remove(secondHandInfo);
		} else {// 添加收藏
			relation.add(secondHandInfo);
		}
		GlobalParams.userInfo.setSecondHandLoves(relation);
		GlobalParams.userInfo.update(this, new UpdateListener() {// 更新

					@Override
					public void onSuccess() {
						dialog.dismiss();
						if (isLove) {
							showToast("已取消收藏~", true);
							isLove = false;
						} else {
							showToast("已添加收藏~", true);
							isLove = true;
						}
						if (isLove) {
							loveMenu.setTitle("取消收藏");
							loveMenu.setResource(R.drawable.icon_menu_love_yes);
						} else {
							loveMenu.setTitle("添加收藏");
							loveMenu.setResource(R.drawable.icon_menu_runnning_love);
						}
					}

					@Override
					public void onFailure(int code, String message) {
						dialog.dismiss();
						LogcatUtils.e("更新收藏失败," + code + "：" + message);
						if (isLove) {
							showToast("取消收藏失败~", false);
						} else {
							showToast("添加收藏失败~", false);
						}
					}
				});
	}
}
